//
//  WWComicTableCell.m
//  YiQiTingShu
//
//  Created by WeiWei on 15/11/10.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "WWComicTableCell.h"
#import <UIImageView+WebCache.h>
#import "PublicHeader.h"

@interface WWComicTableCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *userLabel;
@property (nonatomic, strong) UIImageView *userImageView;
@property (nonatomic, strong) UIImageView *picImageView;
@property (nonatomic, assign) NSInteger height;

@end

@implementation WWComicTableCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_titleLabel];
        _userImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_userImageView];
        _userLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_userLabel];
        _picImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_picImageView];
        [self.titleLabel NightWithType:UIViewColorTypeNormal];
        [self.userLabel NightWithType:UIViewColorTypeNormal];
        [self.contentView NightWithType:UIViewColorTypeNormal];
    }
    return self;
}

- (void)setModel:(WWComicModel *)model {
    if (_model != model) {
        _model = model;
    }
    self.titleLabel.text = [NSString stringWithFormat:@"%@", model.text];
    self.titleLabel.numberOfLines = 0;
    NSArray *arr = [model.u objectForKey:@"header"];
    [self.userImageView sd_setImageWithURL:arr[0] placeholderImage:[UIImage imageNamed:@"placeholderZh.png"]];
    self.userLabel.text = [model.u objectForKey:@"name"];
    _height = 0;
    if ([model.type isEqualToString:@"gif"]) {
        [self.picImageView sd_setImageWithURL:[NSURL URLWithString:[model.gif objectForKey:@"images"][0]] placeholderImage:[UIImage imageNamed:@"placeholderZh.png"]];
        _height = [[model.gif objectForKey:@"height"] integerValue];
    } else {
        if ([[model.image objectForKey:@"small"] count] != 0) {
            [self.picImageView sd_setImageWithURL:[NSURL URLWithString:[model.image objectForKey:@"small"][0]] placeholderImage:[UIImage imageNamed:@"placeholderZh.png"]];
            _height = [[model.image objectForKey:@"height"] integerValue] / 2;
        }
        
    }
    self.userLabel.font = [UIFont systemFontOfSize:15];
    self.userImageView.layer.cornerRadius = 10;
    self.userImageView.layer.masksToBounds = YES;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.userImageView.frame = CGRectMake(FitConstant_XorWidth(5), FitConstant_YorHeight(8), FitConstant_XorWidth(20), FitConstant_YorHeight(20));
    self.userLabel.frame = CGRectMake(FitConstant_XorWidth(28), FitConstant_YorHeight(8), FitConstant_YorHeight(150), FitConstant_YorHeight(20));
    self.titleLabel.frame = CGRectMake(FitConstant_XorWidth(4), FitConstant_YorHeight(39), FitConstant_XorWidth(370), FitConstant_YorHeight(20));
    self.picImageView.frame = CGRectMake(0, FitConstant_YorHeight(72), FitConstant_XorWidth(375), FitConstant_YorHeight(_height));
   
}

@end
